<?php

class ConnectionException extends Exception {}

Class class_conexao {
	/*protected*/static $con = null;
	protected $host;
	protected $user;
	protected $pswd;
	protected $dbname;
	protected $port;
	protected $erro;

	/* Constructor */
	function __construct($p_host, $p_port, $p_dbname, $p_user, $p_pswd ) {
		$this->host = $p_host;
		$this->user = $p_user;
		$this->pswd = $p_pswd;
		$this->dbname = $p_dbname;
		$this->port = $p_port;

		if($this->open() == false) {
			throw new ConnectionException();
			
		}

	} 
	
	function __destruct(){  } //Destructor
	
	# --------------------
	# Function to open a connection with postgresql
	function open() {
		$this->con = mysql_connect($this->host, $this->user, $this->pswd);
								 
		if( $this->con ) {
			if( mysql_select_db($this->dbname, $this->con) ) {
				return $this->con;
			}
			return false;
		}
	  return false;
	}

	# --------------------
	# Function to end a connection
	function close() {		
		mysql_close($this->con);
	}

	# --------------------
	# Function to end a connection
	function execute_query($p_query) {
		$result = mysql_query($p_query, $this->con);
			if($result == false) { 
				$erromsg = mysql_error($this->con); //Get last erro
				return $erromsg;
			} else {
				if(!is_bool($result))
				{
					while($res = mysql_fetch_assoc($result)) {
						$row[] = $res;
					}
					return $row;
				} else {
					return $result;
				}
				 
			}
	}

}

?>